package cool.taomu.toolkit.task.dsl

import cool.taomu.toolkit.platform.inter.AbsPlatform
import com.google.auto.service.AutoService
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.platform.inter.IPlatform
import com.google.inject.TypeLiteral
import java.util.Vector
import java.io.InputStream
import com.google.inject.Key
import com.google.inject.name.Names
import java.io.ByteArrayInputStream

@AutoService(IPlatform)
class TaskDslPlugin extends AbsPlatform{
    
    override note() {
        "Task DSL 插件"
    }
    
    override packages() {
    }
    
    override module(TaomuConfigure config) {
    }
    
    override start() {
    }
    
    override init() {
         if (this.context.isExistingBinding(new TypeLiteral<Vector<InputStream>>() {
        }, "taomu.toolkit.groovy.dsl.input.stream")) {
            var script = '''
            
            import cool.taomu.toolkit.task.dsl.Task; 
            import com.google.inject.name.Names;
            import com.google.inject.Key;
            
            TaomuTask.metaClass.task = {Closure config ->
                
                config.delegate = new Task() 
                config.resolveStrategy = Closure.DELEGATE_FIRST
                config.call()
            }'''
            var inputs = this.context.getInstance(Key.get(new TypeLiteral<Vector<InputStream>>() {
            }, Names.named("taomu.toolkit.groovy.dsl.input.stream")))
            inputs.add(new ByteArrayInputStream(script.bytes))
        }
    }
    
}